package com.hqyj.am.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.am.dao.DepartmentMapper;
import com.hqyj.am.dao.RoleMapper;
import com.hqyj.am.dao.UserRoleMapper;
import com.hqyj.am.pojo.*;
import com.hqyj.am.dao.UserMapper;
import com.hqyj.am.service.IUserRoleService;
import com.hqyj.am.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.am.util.MDFive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import com.hqyj.am.pojo.User;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
<<<<<<< HEAD
 * @since 2022-08-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Autowired
    private IUserRoleService iUserRoleService;

    @Autowired
    private MDFive mdFive;

    @Override
    public ResultVo queryUser(MyPage myPage) {
        ResultVo rv = new ResultVo();
//        Page<MyPage> mp = new Page<>(myPage.getPage(), myPage.getRow());
//        Page<MyPage> my = userMapper.queryUser(mp, myPage.getUserName(), myPage.getDepartment(), myPage.getUserNum());
//        List<Department> departments = departmentMapper.selectList(null);
//        List<Role> roles = roleMapper.selectList(null);
//        Map<String, Object> map = new HashMap<>();
//        map.put("list", my.getRecords());
//        map.put("departments", departments);
//        map.put("roles", roles);
//        map.put("totalPage", my.getPages());
//        map.put("total", my.getTotal());
//        rv.setData(map);
//        rv.setSuccess(true);
        return rv;
    }

    @Override
    public ResultVo delUser(Integer userId) {
        ResultVo rv = new ResultVo();
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        boolean userId0 = userId != null;
        wrapper.eq(userId0, "user_id", userId);
        int i = userMapper.deleteById(userId);
        if (i > 0) {
            userRoleMapper.delete(wrapper);
            rv.setInfo("删除成功");
            rv.setSuccess(true);
        } else {
            rv.setSuccess(false);
            rv.setInfo("删除失败");
        }
        return rv;
    }

    @Override
    public ResultVo queryRoleAndDepartment() {
        ResultVo rv = new ResultVo();
        QueryWrapper<Department> wrapper0 = new QueryWrapper<>();
        QueryWrapper<Role> wrapper1 = new QueryWrapper<>();
        List<Object> list = new LinkedList<>();
        List<Department> departments = departmentMapper.selectList(wrapper0);
        List<Role> roles = roleMapper.selectList(wrapper1);
        list.add(departments);
        list.add(roles);
        rv.setSuccess(true);
        rv.setData(list);
        return rv;
    }

    @Override
    public ResultVo addUser(User user) {
        ResultVo rv = new ResultVo();
        String s = "135510";
        while (true) {
            Random rd = new Random();
            String num = rd.nextInt(900) + 100 + "";
            s += num;
            user.setUserNum(s);
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            boolean userNum = user.getUserNum() != null && !user.getUserNum().equals("");
            wrapper.eq(userNum, "user_num", user.getUserNum());
            User user1 = userMapper.selectOne(wrapper);
            if (user1 == null) {
                break;
            }
        }
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        boolean department = user.getDepartment() != null && !user.getDepartment().equals("");
        wrapper.eq(department, "department_name", user.getDepartment());
        Department department1 = departmentMapper.selectOne(wrapper);
        user.setDepartmentId(department1.getDepartmentId());
        String salt = UUID.randomUUID() + "";
        user.setSalt(salt);
        user.setUserPwd(mdFive.encrypt(user.getUserPwd(), salt));
        int i = userMapper.addUser(user);
        String ros = user.getRoles();
        String[] ros1 = ros.split(",");
        if (i > 0) {
            if (ros1.length > 1) {
                List<UserRole> roles = new LinkedList<>();
                for (String s1 : ros1) {
                    UserRole ur = new UserRole();
                    ur.setRoleId(Integer.parseInt(s1));
                    ur.setUserId(user.getUserId());
                    roles.add(ur);
                }
                iUserRoleService.saveBatch(roles);
            } else {
                UserRole userRole = new UserRole();
                userRole.setUserId(user.getUserId());
                userRole.setRoleId(Integer.parseInt(ros1[0]));
                userRoleMapper.insert(userRole);
            }
            rv.setInfo("新增成功");
            rv.setSuccess(true);
        } else {
            rv.setInfo("新增失败");
            rv.setSuccess(false);
        }
        return rv;
    }

    @Override
    public ResultVo showUser(Integer userId) {
        ResultVo rv = new ResultVo();
        QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
        boolean userId0 = userId != null;
        wrapper.eq(userId0, "user_id", userId);
        List<UserRole> userRoles = userRoleMapper.selectList(wrapper);
        List<Role> list = new LinkedList<>();
        for (UserRole userRole : userRoles) {
            QueryWrapper<Role> wrapper1 = new QueryWrapper<>();
            boolean isRole = userRole.getRoleId() != null;
            wrapper1.eq(isRole, "role_id", userRole.getRoleId());
            Role role = roleMapper.selectOne(wrapper1);
            list.add(role);
        }
        User user = userMapper.showUser(userId);
        user.setRoles1(list);
        rv.setData(user);
        return rv;
    }

    @Override
    public ResultVo updateUser(User user) {
        ResultVo rv = new ResultVo();

        int i = userMapper.updateById(user);
        String ros = user.getRoles();
        String[] ros1 = ros.split(",");
        QueryWrapper<User> wrapper1 = new QueryWrapper<>();
        boolean userNum = user.getUserNum() != null && !user.getUserNum().equals("");
        wrapper1.eq(userNum, "user_num", user.getUserNum());
        User user1 = userMapper.selectOne(wrapper1);
        if (user1 == null) {
            if (i > 0) {
                QueryWrapper<UserRole> wrapper = new QueryWrapper<>();
                boolean userId0 = user.getUserId() != null;
                wrapper.eq(userId0, "user_id", user.getUserId());
                userRoleMapper.delete(wrapper);
                if (ros1.length > 1) {
                    List<UserRole> roles = new LinkedList<>();
                    for (String s1 : ros1) {
                        UserRole ur = new UserRole();
                        ur.setRoleId(Integer.parseInt(s1));
                        ur.setUserId(user.getUserId());
                        roles.add(ur);
                    }
                    iUserRoleService.saveBatch(roles);
                } else {
                    UserRole userRole = new UserRole();
                    userRole.setUserId(user.getUserId());
                    userRole.setRoleId(Integer.parseInt(ros1[0]));
                    userRoleMapper.insert(userRole);
                }
                rv.setInfo("修改成功");
                rv.setSuccess(true);
            } else {
                rv.setInfo("修改失败");
                rv.setSuccess(false);
            }
        } else {
            rv.setInfo("修改失败");
            rv.setSuccess(false);
        }
        return rv;
    }
}
